package com.my.selectcoursesystem.controller;

import com.my.selectcoursesystem.bean.Course;
import com.my.selectcoursesystem.bean.SC;
import com.my.selectcoursesystem.bean.SelectCourse;
import com.my.selectcoursesystem.service.TeacherService;
import com.my.selectcoursesystem.service.serviceImpl.CourseServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.SCServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.SelectCourseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CourseController {

    @Autowired
    private CourseServiceImpl courseService;
    @Autowired
    private SelectCourseServiceImpl selectCourseService;
    @Autowired
    private SCServiceImpl scService;
    @Autowired
    private TeacherService teacherService;

    public CourseController(CourseServiceImpl courseService, SelectCourseServiceImpl selectCourseService, SCServiceImpl scService, TeacherService teacherService) {
        this.courseService = courseService;
        this.selectCourseService = selectCourseService;
        this.scService = scService;
        this.teacherService = teacherService;
    }

    public CourseController() {
    }

    // 获取cookie
    public String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();//根据请求数据，找到cookie数组
        String value = "";
        if (null == cookies) {//如果没有cookie数组
            System.out.println("没有cookie");
            value = "";
        } else {
            for (Cookie cookie : cookies) {
                System.out.println("cookieName:" + cookie.getName() + ",cookieValue:" + cookie.getValue());
                if (cookie.getName().equals(name)) {
                    value = cookie.getValue();
                    break;
                }
            }
        }
        return value;
    }

    // 查看选课课程列表
    @RequestMapping("/selcectCourse")
    public String showAllCourse(Model model) {
        List<Course> courses = this.courseService.getAllCourse();
        model.addAttribute("courseList", courses);
        return "student/selCourse";
    }

    // 查看选课结果
    @RequestMapping(value = "/showCourse/{stu_id}", method = RequestMethod.GET)
    public String showSelCourse(@PathVariable(value = "stu_id") String stu_id, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        List<SelectCourse> ssrList = this.selectCourseService.getSelcOfStu(stu_id);
        model.addAttribute("ssrList", ssrList);
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        return "student/selectedCourse";
    }

    // 查询课程
    @RequestMapping(value = "/query")
    public String queryCourse(String serc, String condition, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        switch (serc) {
            case "all":
            case "nam":
                List<Course> courses = this.courseService.searchByCourseName(condition);
                model.addAttribute("courseList", courses);
                break;
            case "cid":
                List<Course> courses1 = this.courseService.searchByCid(condition);
                model.addAttribute("courseList", courses1);
                break;
            case "col":
                List<Course> courses3 = this.courseService.searchByCollege(condition);
                model.addAttribute("courseList", courses3);
                break;
            case "type":
                List<Course> courses4 = this.courseService.searchByType(condition);
                model.addAttribute("courseList", courses4);
                break;
            default:
                break;
        }
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        return "student/selCourse";
    }

    // 确认选课
    @RequestMapping(value = "/selected")
    public String selectCourse(String cid, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        SC sc1 = this.scService.getSCByCidAndSid(cid, sid);
        if (sc1 != null) {
            model.addAttribute("msg", "您已经选过该课程了!");
            model.addAttribute("u", "student");
            return "success";
        }
        SC sc = new SC();
        sc.setCouid(cid);
        sc.setStuid(sid);
        SC insertSC = this.scService.insertSC(sc);
        if (insertSC != null) {
            model.addAttribute("u", "student");
            return "success";
        } else {
            model.addAttribute("msg", "选课失败!");
            model.addAttribute("u", "student");
            return "fail";
        }
    }

    // 选课确认
    @RequestMapping(value = "/select/{cid}")
    public String goSelectCourse(@PathVariable(value = "cid") String cid, Model model) {
        model.addAttribute("cid", cid);
        Course course = this.courseService.getCourseById(cid);
        model.addAttribute("course", course);
        model.addAttribute("teacher", this.teacherService.getTeacherById(course.getTeacherid()));
        return "student/seling";
    }

    // 推选页面
    @RequestMapping(value = "/exitCourse/{sid}")
    public String goExitCourse(@PathVariable(value = "sid") String sid, Model model, HttpServletRequest request) {
        List<SelectCourse> ssrList = this.selectCourseService.getSelcOfStu(sid);
        model.addAttribute("sid", sid);
        model.addAttribute("sname", this.getCookie(request, "sname"));
        model.addAttribute("ssrList", ssrList);
        return "student/exitSel";
    }

    // 确认推选
    @RequestMapping(value = "/exitedCourse/{cid}")
    public String exitCourse(@PathVariable(value = "cid") String cid, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        Boolean isDel = this.scService.deleteSCByCidAndSid(cid, sid);
        if (isDel) {
            model.addAttribute("msg", "退选成功!");
            model.addAttribute("u", "student");
            return "success";
        } else {
            model.addAttribute("msg", "退选失败!");
            model.addAttribute("u", "student");
            return "fail";
        }
    }

}
